<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="请输入任务标题" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="">任务类型</option>
                            <option value="1">学习任务</option>
                            <option value="2">其他任务</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="edit"><i class="layui-icon layui-icon-edit "></i></button>
</script>
<script type="text/html" id="table-status">
    {{# if(d.hid_status == 2){ }}
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="table-status" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.status == 1 ? 'checked' : '' }}>
    {{#  } else { }}
    {{#if (d.status == '1') { }}
    <span>开启</span>
    {{# }else if(d.status == '2'){ }}
    <span>关闭</span>
    {{# } }}
    {{# } }}
</script>
<script type="text/html" id="table-type">
    {{#if (d.type == '1') { }}
    <span>学习任务</span>
    {{# }else if(d.type == '2'){ }}
    <span>其他任务</span>
    {{# } }}
</script>

<script type="text/html" id="table-range">
    {{#if (d.range == '1') { }}
    <span>每天</span>
    {{# }else if(d.range == '2'){ }}
    <span>每周</span>
    {{# }else if(d.range == '3'){ }}
    <span>每月</span>
    {{# }else if(d.range == '4'){ }}
    <span>每年</span>
    {{# }else if(d.range == '5'){ }}
    <span>一次性</span>
    {{# } }}
</script>
<script type="text/html" id="user-img">
    <div class="user-info-head">
        <img src="{{d.icon}}" width="30px" height="30px" alt="">
    </div>
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let cols = [
            [{
                    title: 'ID',
                    field: 'id',
                    align: 'center'
                },
                {
                    title: '排序',
                    field: 'sort',
                    align: 'center',
                    edit:'text'
                },
                {
                    title: '任务标题',
                    field: 'title',
                    align: 'center'
                },
                {
                    title: '图标',
                    field: 'icon',
                    align: 'center',
                    templet: '#user-img'
                },
                {
                    title: '任务类型',
                    field: 'type',
                    align: 'center',
                    templet:'#table-type',
                },
                {
                    title: '任务周期',
                    field: 'range',
                    align: 'center',
                    templet:'#table-range',
                },
                {
                    title: '状态',
                    field: 'status',
                    align: 'center',
                    templet:'#table-status',
                },
                {
                    title: '奖励积分',
                    field: 'integral',
                    align: 'center'
                },
                {
                    title: '奖励次数',
                    field: 'num',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/IntegralTask/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        //监听锁定操作
        form.on('switch(table-status)', function(obj) {
            $.post("{:url('toggle')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });

        //监听单元格编辑
        table.on('edit(user-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
            $.post("{:url('sort')}", {id: data.id,sort:value}, function (result) {
                if(result.status == 0) {
                    table.reload("user-table");
                } else {
                    layer.msg(result.msg,{icon:2,time:1000});
                    table.reload("user-table");
                }
            });
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/IntegralTask/edit?id='+obj.data.id
            });
        }


        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
